package cn.wolfcode.web.controller;

import cn.wolfcode.ann.RequiredPermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private  IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    public RoleController(IRoleService roleService) {
        this.roleService = roleService;
    }
    @RequiredPermission(name="角色页面",expression = "role:list")

    @RequestMapping("/list")
    //Model 用来封装数据
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions", permissions);

        return "role/list";
    }
//    public String list(Model model){
////        List<Role> roleList=roleService.selectAll();
////        model.addAttribute("roleList",roleList);
////        return "role/list";
//    }
    //// 去新增或去修改页面
@RequiredPermission(name="新增/编辑",expression = "role:saveOrUpdate")
@RequestMapping("/input")
    public String input(Long id, Model model){
        if (id!=null){
            Role role=roleService.get(id);
            model.addAttribute("role",role);
        }
        return "role/input";
    }
    @RequiredPermission(name="新增/编辑",expression = "role:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role){
       if (role.getId()==null){
           roleService.save(role);
       }else {
           roleService.update(role);
       }
        return "redirect:/role/list";
    }
    @RequiredPermission(name="角色删除",expression = "role:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            roleService.delete(id);
        }
        return "redirect:/role/list";
    }
}
